{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Question 90\n",
    "\n",
    "### **Question**\n",
    "\n",
    "> **_Please write a program which count and print the numbers of each character in a string input by console._**\n",
    "\n",
    "> **_Example:\n",
    "> If the following string is given as input to the program:_**\n",
    "\n",
    "```\n",
    "abcdefgabc\n",
    "```\n",
    "\n",
    "> **_Then, the output of the program should be:_**\n",
    "\n",
    "```\n",
    "a,2\n",
    "c,2\n",
    "b,2\n",
    "e,1\n",
    "d,1\n",
    "g,1\n",
    "f,1\n",
    "```\n",
    "\n",
    "### Hints\n",
    "\n",
    "> **_Use dict to store key/value pairs.\n",
    "> Use dict.get() method to lookup a key with default value._**\n",
    "\n",
    "---\n",
    "\n",
    "**Solutions:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import string\n",
    "\n",
    "s = input()\n",
    "for letter in string.ascii_lowercase:\n",
    "    cnt = s.count(letter)\n",
    "    if cnt > 0:\n",
    "        print(\"{},{}\".format(letter, cnt))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**OR**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = input()\n",
    "for letter in range(ord(\"a\"), ord(\"z\") + 1):  # ord() gets the ascii value of a char\n",
    "    letter = chr(letter)  # chr() gets the char of an ascii value\n",
    "    cnt = s.count(letter)\n",
    "    if cnt > 0:\n",
    "        print(\"{},{}\".format(letter, cnt))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "# Question 91\n",
    "\n",
    "### **Question**\n",
    "\n",
    "> **_Please write a program which accepts a string from console and print it in reverse order._**\n",
    "\n",
    "> **Example:\n",
    "> If the following string is given as input to the program:\\***\n",
    "\n",
    "\n",
    "rise to vote sir\n",
    "\n",
    "\n",
    "> **_Then, the output of the program should be:_**\n",
    "\n",
    "ris etov ot esir\n",
    "\n",
    "### Hints\n",
    "\n",
    "> **_Use list[::-1] to iterate a list in a reverse order._**\n",
    "\n",
    "---\n",
    "\n",
    "**Solutions:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = input()\n",
    "s = \"\".join(reversed(s))\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "# Question 92\n",
    "\n",
    "### **Question**\n",
    "\n",
    "> **_Please write a program which accepts a string from console and print the characters that have even indexes._**\n",
    "\n",
    "> **_Example:\n",
    "> If the following string is given as input to the program:_**\n",
    "```\n",
    "H1e2l3l4o5w6o7r8l9d\n",
    "```\n",
    "> **_Then, the output of the program should be:_**\n",
    "```\n",
    "Helloworld\n",
    "```\n",
    "### Hints\n",
    "\n",
    "> **_Use list[::2] to iterate a list by step 2._**\n",
    "\n",
    "---\n",
    "\n",
    "\n",
    "\n",
    "**Solutions:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = \"H1e2l3l4o5w6o7r8l9d\"\n",
    "s = [s[i] for i in range(len(s)) if i % 2 == 0]\n",
    "print(\"\".join(s))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**OR**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = \"H1e2l3l4o5w6o7r8l9d\"\n",
    "ns = \"\"\n",
    "for i in range(len(s)):\n",
    "    if i % 2 == 0:\n",
    "        ns += s[i]\n",
    "print(ns)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "# Question 93\n",
    "\n",
    "### **Question**\n",
    "\n",
    "> **_Please write a program which prints all permutations of [1,2,3]_**\n",
    "\n",
    "---\n",
    "\n",
    "### Hints\n",
    "\n",
    "> **_Use itertools.permutations() to get permutations of list._**\n",
    "\n",
    "---\n",
    "\n",
    "**Solution:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]\n"
     ]
    }
   ],
   "source": [
    "import itertools\n",
    "\n",
    "print(list(itertools.permutations([1, 2, 3])))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "# Question 94\n",
    "\n",
    "### **Question**\n",
    "\n",
    "> **_Write a program to solve a classic ancient Chinese puzzle:\n",
    "> We count 35 heads and 94 legs among the chickens and rabbits in a farm. How many rabbits and how many chickens do we have?_**\n",
    "\n",
    "---\n",
    "\n",
    "### Hints\n",
    "\n",
    "> **_Use for loop to iterate all possible solutions._**\n",
    "\n",
    "---\n",
    "\n",
    "**Solution:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "code_folding": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(23, 12)\n"
     ]
    }
   ],
   "source": [
    "def solve(numheads, numlegs):\n",
    "    ns = \"No solutions!\"\n",
    "    for i in range(numheads + 1):\n",
    "        j = numheads - i\n",
    "        if 2 * i + 4 * j == numlegs:\n",
    "            return i, j\n",
    "    return ns, ns\n",
    "\n",
    "\n",
    "numheads = 35\n",
    "numlegs = 94\n",
    "solutions = solve(numheads, numlegs)\n",
    "print(solutions)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
